import { fetchFile, createFFmpeg } from '@ffmpeg/ffmpeg'
import store from '@/store'
// @ts-ignore
// const { fetchFile, createFFmpeg } = FFmpeg

const ffmpeg = createFFmpeg({
    corePath: 'http://localhost:8080/plugin/ffmpeg-core.js',
    // corePath: 'https://cdn.bootcdn.net/ajax/libs/ffmpeg-core/0.11.0/ffmpeg-core.js',
    log: true
})
ffmpeg.load()
ffmpeg.setProgress(progress => {
    store.commit('setProcessData', progress.ratio);
})

const videoInfo = {
    duration: '',
    bitRate: ''
}

ffmpeg.setLogger(logs => {
    if (logs.type === 'info' && logs.message.includes('ffmpeg-core loaded')) {
        store.commit('setLoaded', true);
    }
    if (logs.message.includes('Duration')) {
        videoInfo.duration = logs.message.slice(
            logs.message.indexOf('Duration:') + 'Duration: '.length,
            logs.message.indexOf(',')
        )
        videoInfo.bitRate = logs.message.slice(
            logs.message.indexOf('bitrate:') + 'bitrate: '.length
        )
        console.log(videoInfo)
    }
})

let videoName = 'initVideo'
/**ffmpeg导入视频 */
export const initVideo = async (video) => {
    ffmpeg.FS('writeFile', videoName, await fetchFile(video))
    await ffmpeg.run('-i', videoName)
}

let fontName = 'font1'
/** ffmpeg导入字体 */
export const writeFontFile = async (font) => {
    ffmpeg.FS('writeFile', fontName, await fetchFile(font))
}

let imageName = 'imageMark'
/**ffmpeg导入贴图 */
export const writeImage = async (image) => {
    ffmpeg.FS('writeFile', imageName, await fetchFile(image))
}

let subTitle = 'subtitle.srt'
/**ffmpeg导入字幕文件 */
export const writeSubTitle = async (subtitle) => {
    ffmpeg.FS('writeFile', subTitle, await fetchFile(subtitle))
}

/**通过url获取文件blob数据 */
export const urlGetData = async (fileUrl, type = 'video/mp4') => {
    const tmp = 'tmpFile'
    ffmpeg.FS('writeFile', tmp, await fetchFile(fileUrl))
    const outputData = ffmpeg.FS('readFile', tmp)
    return new Blob([outputData.buffer], { type })
}

/** 切分视频 */
export const ffmpegSliceVideo = async (
    fileUrl,
    fileName,
    startTime,
    endTime
) => {
    ffmpeg.FS('writeFile', fileName, await fetchFile(fileUrl))
    // 将视频分割为两个部分
    // const command = `-i ${fileName} -t ${startTime} -c copy output1.mp4 -ss ${startTime} -c copy output2.mp4`
    // const command = `-i ${fileName} -t ${endTime} -c copy output1.mp4`
    const command = `-i ${fileName} -t ${endTime} -c copy output1.mp4 -ss ${endTime} -c copy output2.mp4`
    console.log("+++++++++++++++++++++++++++++++++++++", command);
    await ffmpeg.run(...command.split(' '))

    const video1 = ffmpeg.FS('readFile', 'output1.mp4')
    console.log("#####################", video1);

    const video1Url = URL.createObjectURL(
        new Blob([video1.buffer], { type: 'video/mp4' })
    )
    console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%", video1Url);

    // const video2 = ffmpeg.FS('readFile', 'output2.mp4')
    // const video2Url = URL.createObjectURL(
    //   new Blob([video2.buffer], { type: 'video/mp4' })
    // )

    ffmpeg.FS('writeFile', fileName, await fetchFile(video1Url))
    const command2 = `-i ${fileName} -t ${startTime} -c copy output3.mp4 -ss ${startTime} -c copy output4.mp4`
    await ffmpeg.run(...command2.split(' '))
    // const video3 = ffmpeg.FS('readFile', 'output3.mp4')
    // const video3Url = URL.createObjectURL(
    //   new Blob([video3.buffer], { type: 'video/mp4' })
    // )
    const video4 = ffmpeg.FS('readFile', 'output4.mp4')
    const video4Url = URL.createObjectURL(
        new Blob([video4.buffer], { type: 'video/mp4' })
    )
    return [video4Url]
}

/**给视频添加字幕 */
export const addSubTitle = async (fileUrl, fileName) => {
    ffmpeg.FS('writeFile', fileName, await fetchFile(fileUrl))
    const cmd = `-i ${fileName} -vf subtitles=${subTitle} output.mp4`

    await ffmpeg.run(...cmd.split(' '))

    const outputData = ffmpeg.FS('readFile', 'output.mp4')

    const outputBlob = new Blob([outputData.buffer], { type: 'video/mp4' })
    return URL.createObjectURL(outputBlob)
}

/** 获取视频的每一秒帧 */
export const gVideoFrame = async (
    fileUrl,
    timeArr = [],
    videoName = 'initVideo',
    tickCounts
) => {
    const frameDir = videoName
    ffmpeg.FS('writeFile', videoName, await fetchFile(fileUrl))
    ffmpeg.FS('mkdir', frameDir + 'Frame')

    const second = tickCounts / timeArr[timeArr.length - 1]
    let cmd = `-i ${videoName} -vf fps=${second} -q:v 5 -s 320x240 -an -preset fast /${frameDir}Frame/%3d.jpeg -hide_banner`
    let args = cmd.split(' ')
    await ffmpeg.run(...args)
    const fileList = ffmpeg.FS('readdir', '/' + frameDir + 'Frame')
    let urls = [];
    fileList.forEach(v => {
        if (v !== '.' && v !== '..') {
            const path = frameDir + 'Frame' + '/' + v
            const img = ffmpeg.FS('readFile', path)
            let url = URL.createObjectURL(
                new Blob([img.buffer], { type: 'image/jpeg' })
            )
            urls.push({
                url
            })
        }
    })
    return urls
}

/** 给视频添加文字 */
export const addText = async (
    fileUrl,
    videoName = 'initVideo',
    text = 'hello',
    startT,
    endT
) => {
    ffmpeg.FS('writeFile', videoName, await fetchFile(fileUrl))
    const cmd = `-re -i ${videoName} -vf`
    const textT =
        `drawtext=fontfile=font1:text=${text}` +
        `:fontcolor=white:fontsize=80:x=100:y=10:box=1:boxcolor=#0000007d:enable='between(t,${startT},${endT})'`
    let args = cmd.split(' ')
    await ffmpeg.run(...args, textT, 'outfile.mp4')
    const data = ffmpeg.FS('readFile', 'outfile.mp4')
    return URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }))
}

/** 给视频添加贴图 */
export const addImage = async (
    fileUrl,
    videoName = 'initVideo',
    startT,
    endT
) => {
    ffmpeg.FS('writeFile', videoName, await fetchFile(fileUrl))
    const cmd = `-i ${videoName} -i ${imageName} -filter_complex overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2:enable='between(t,${startT},${endT})' outfile.mp4`
    let args = cmd.split(' ')
    await ffmpeg.run(...args, '-hide_banner')
    const data = ffmpeg.FS('readFile', 'outfile.mp4')
    return URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }))
}

/** 获取视频的第一帧图片 */
export const getFirstFrame = async (
    fileUrl,
    fileName,
    initTime = '00:00:00.001'
) => {
    ffmpeg.FS('writeFile', fileName, await fetchFile(fileUrl))
    console.log('视频的第一帧图片')
    await ffmpeg.run(
        '-hwaccel',
        'auto',
        '-i',
        fileName,
        '-ss',
        initTime,
        '-vframes',
        '1',
        '-s',
        '640x480',
        '-an',
        '-threads',
        '4',
        '-preset',
        'fast',
        'output.jpg'
    )
    const data = ffmpeg.FS('readFile', 'output.jpg')
    const url = URL.createObjectURL(
        new Blob([data.buffer], { type: 'image/jpeg' })
    )
    return {
        url,
        videoInfo: JSON.stringify(videoInfo)
    }
}

/**导出视频，降帧 */
export const videoLower = async (fileUrl, fileName) => {
    ffmpeg.FS('writeFile', fileName, await fetchFile(fileUrl))
    const cmd = `-i ${fileName} -b:v 2000k -q:v 2 -r 24 -s 1240x960 output.mp4`
    let args = cmd.split(' ')
    await ffmpeg.run(...args)
    const data = ffmpeg.FS('readFile', 'output.mp4')
    const url = URL.createObjectURL(
        new Blob([data.buffer], { type: 'video/mp4' })
    )
    return url
}
